import mAppInfo from '../appInfo'
import {
	stringify,
	Decrypt,
	stringifyEncrypt
} from '../util/netSecret'
// import {
// 	Decrypt,
// 	Encrypt,
// } from '@/common/js/secret.js'

function NetTool() {
	this.appInfo = mAppInfo
	//数据请求
	this._getAjaxUrlValueBackground = function(url, data, fn, isBackground, failCallback) { 
	 
		var that = this;
		// 加载model
		if (!isBackground) {
			// uni.showLoading({
			// 	title: '加载中...',
			// })
		}
		let token = uni.getStorageSync("token")
		if(!token){
			// console.log(
			// 	' ..........................没有Token了......................\n' +
			// 	'                       _oo0oo_                      \n' +
			// 	'                      o8888888o                     \n' +
			// 	'                      88" . "88                     \n' +
			// 	'                      (| -_- |)                     \n' +
			// 	'                      0\\  =  /0                     \n' +
			// 	'                   ___/‘---’\\___                   \n' +
			// 	"                  .' \\|       |/ '.                 \n" +
			// 	'                 / \\\\|||  :  |||// \\                \n' +
			// 	'                / _||||| -卍-|||||_ \\               \n' +
			// 	'               |   | \\\\\\  -  /// |   |              \n' +
			// 	"               | \\_|  ''\\---/''  |_/ |              \n" +
			// 	"               \\  .-\\__  '-'  ___/-. /              \n" +
			// 	"             ___'. .'  /--.--\\  '. .'___            \n" +
			// 	'         ."" ‘<  ‘.___\\_<|>_/___.’>’ "".          \n' +
			// 	'       | | :  ‘- \\‘.;‘\\ _ /’;.’/ - ’ : | |        \n' +
			// 	'         \\  \\ ‘_.   \\_ __\\ /__ _/   .-’ /  /        \n' +
			// 	'    =====‘-.____‘.___ \\_____/___.-’___.-’=====     \n' +
			// 	'                       ‘=---=’                      \n' +
			// 	'                                                    \n' +
			// 	'...................没有Token了 ,请获取...................'
			// );
			
			 
		}
		// 添加版本控制 
		var arrData = {
			business_id: this.appInfo.business_id,
			shop_id: uni.getStorageSync('shop_id') ? uni.getStorageSync('shop_id') : this.appInfo.shop_id,
			// user_id: uni.getStorageSync('user_id'),
			config_business_id: this.appInfo.config_business_id,
			...data,
			token: token
		}
		if (data.user_id == '' || data.user_id == "") {
			delete arrData.user_id;
		}
		if (data.shop_id == '' || data.shop_id == "") {
			delete arrData.shop_id;
		}
		arrData.maskTime = new Date().getTime();//必须是13位
		 console.log('上传 的数据',arrData)
		let __data = stringifyEncrypt(arrData,  this.appInfo.appKey) //加密
		// let __data = stringifyEncrypt(arrData) //加密
		
		__data.version = 3.0
		__data.token = token;
		__data.appKey = this.appInfo.appKey;
		__data.appOpenId = this.appInfo.appOpenId;
		// if (url.indexOf('capi') > -1) {
		// 	let _url = url.replace(/\//g, '');
		// 	_url = _url.replace(/capi/g, '')
		// 	_url = _url.replace('V2', '')
		// 	__data.api_address = _url;
		// }
		// let __data2 = Decrypt(__data,  "1111111111111111", this.appInfo.appKey) //解密
	
		uni.request({
			url: this.appInfo.apiUrl + url,
			data: __data,
			method: "GET",
			success: function(res) {  
				// res = Decrypt(res)//解密
			 
				// typeof fn == "function" && fn(res);
				if (res.data.success) {
					if (res.data.data) {
						typeof fn == "function" && fn(res.data.data);
					} else if (res.data) {
						typeof fn == "function" && fn(res.data);
					} else {
						typeof fn == "function" && fn(res);
					}
				} else {
					if (res.data.code == 10000) {
						uni.removeStorageSync("token");
						uni.removeStorageSync("user_id");
						uni.removeStorageSync("userInfo");
						return;
					} else {
						if (failCallback) {
							typeof failCallback == "function" && failCallback(res.data);
							return;
						}
					}

					if (res.data && res.data.token) {
						typeof fn == "function" && fn(res.data);
						return;
					}
					if (res.data.msg && res.data.msg != '') {
						uni.showToast({
							title: res.data.msg,
							icon: "none",
							duration: 3000,
						})
					}
				}
			},
			fail: function(XMLHttpRequest) {
				console.log('请求失败，原因',XMLHttpRequest)
				console.log('XMLHttpRequest',XMLHttpRequest)
				uni.hideLoading();
				uni.showToast({
					title: "网络连接超时",
					icon: 'success',
					image: "/images/info.png",
					duration: 2000
				})
			},
			complete: function(res) {
				// uni.hideLoading();
				
			}
		});
	};


	this.getAjaxUrlValue = function(url, data, fn, fail) {
	
		this._getAjaxUrlValueBackground(url, data, fn, false, fail)
	};

	this.getAjax = function(url, data, fn, fail) {

		this._getAjaxUrlValueBackground(url, data, fn, false, fail)
	};
	this.getAjaxUrlValueBackground = function(url, data, fn, fail) {
		
		this._getAjaxUrlValueBackground(url, data, fn, true, fail)
	};

}

module.exports = new NetTool();
